package com.xhwl.data.config;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.exception.DataException;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.Industry;
import com.xhwl.common.utils.StringUtils;
import com.xhwl.data.service.IIndustryService;
import com.xhwl.data.util.RedisUtils;
import com.xhwl.data.util.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @Author: luyi
 * @Description:
 * @Date: Create in 15:44 2022/7/7
 */
@Component
public class WebIndustryArgResolver implements WebArgumentResolver {

    @Autowired
    private IIndustryService industryService;

    private static final Logger log = LoggerFactory.getLogger(WebIndustryArgResolver.class);
    @Override
    public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest nativeWebRequest) throws Exception {
        if (null != methodParameter.getParameterType() && methodParameter.getParameterType().equals(Industry.class)) {
            HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
            String appId = request.getHeader("industryId");
            if(StringUtils.isEmpty(appId)){
                throw new DataException("head param error");
            }
            Industry in = industryService.getById(Integer.valueOf(appId));
            if(null == in){
                throw new DataException("industry not exist");
            }
            return in;
        }
        return UNRESOLVED;
    }
}
